package com.cn.springcloudwechat.serviceImpl.find;

import com.cn.springcloudwechat.common.utils.Const;
import com.cn.springcloudwechat.common.utils.ErrorUtil;
import com.cn.springcloudwechat.mapper.mapper.YmUserMapper;
import com.cn.springcloudwechat.mapper.mapper.find.askDoctor.YmDoctorLevelMapper;
import com.cn.springcloudwechat.mapper.mapper.find.askDoctor.YmDoctorMapper;
import com.cn.springcloudwechat.mapper.mapper.find.askDoctor.YmDoctorWorkMapper;
import com.cn.springcloudwechat.mapper.mapper.find.askDoctor.YmDouctorConsultTypeMapper;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.find.askDoctor.YmDoctor;
import com.cn.springcloudwechat.model.find.askDoctor.YmDoctorLevel;
import com.cn.springcloudwechat.model.find.askDoctor.YmDoctorWork;
import com.cn.springcloudwechat.model.find.askDoctor.YmDouctorConsultType;
import com.cn.springcloudwechat.service.find.askDoctor.AskDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 问医生- 业务实现层
 *
 */
@Service("AskDoctorService")
public class AskDoctorServiceImpl implements AskDoctorService {

    @Autowired
    YmDoctorMapper ymDoctorMapper;
    @Autowired
    YmDoctorLevelMapper ymDoctorLevelMapper;
    @Autowired
    YmDouctorConsultTypeMapper ymDouctorConsultTypeMapper;
    @Autowired
    YmDoctorWorkMapper ymDoctorWorkMapper;
    @Autowired
    YmUserMapper ymUserMapper;

    /**
     * 问医生- 配置医生信息
     *@param userId,  ymdoctor
     *@return Integer
     */
    @Transactional
    public Integer insertYmDoctor(Long userId, YmDoctor ymDoctor){
        Time workStart = ymDoctor.getWorkStart();
        Time workEnd = ymDoctor.getWorkEnd();
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        Assert.notNull(workStart,String.format(ErrorUtil.PARAMETER_NULL,"workStart"));
        Assert.notNull(workEnd,String.format(ErrorUtil.PARAMETER_NULL,"workEnd"));

        Long stationId = ymDoctor.getStationId();
        if(null == stationId && 0 == stationId){
            Assert.notNull(stationId,String.format(ErrorUtil.PARAMETER_NULL,"stationId"));
        }
        // 判断该用户是否已经被配置为医生
        Integer  number = ymDoctorMapper.queryByUserId(userId);
        YmUser ymUser = ymUserMapper.selectByPrimaryKey(userId);
        if(number == 0){
            // 如果用户未被配置为医生就写入数据
            // 配置医生基本信息
            number = ymDoctorMapper.insertSelective(ymDoctor);
            if(number == 0){
                throw new IllegalArgumentException(ErrorUtil.DOCTOR_ERROR);
            }else{
                // 配置成功修改用户的type 为1(医生)
                Short type = 1;
                ymUser.setType(type);
                ymUserMapper.updateByPrimaryKeySelective(ymUser);
                // 根据用户ID查询doctorId
                YmDoctor ymdoctor = ymDoctorMapper.selectByUserId(userId);
                Long doctorId = ymdoctor.getDoctorId();

                YmDoctorWork ymDoctorWork = new YmDoctorWork();
                // 修改医生上班时间
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                String str = sdf.format(workStart);
                String str1 = sdf.format(workEnd);
                String workTime = str + "-" + str1;
                ymDoctorWork.setDoctorId(doctorId);
                ymDoctorWork.setWorkStart(workStart);
                ymDoctorWork.setWorkTime(workTime);
                ymDoctorWork.setWorkEnd(workEnd);
                ymDoctorWorkMapper.deleteByDoctorId(doctorId);
                ymDoctorWorkMapper.insertSelective(ymDoctorWork);
                return number;
            }
        }else{
            // 如果用户已被配置为医生就抛出异常
            throw new IllegalArgumentException(ErrorUtil.USER_DOCTOR);
        }
    }

    /**
     * 问医生- 查询医生等级配置
     *
     */
    @Override
    public List<YmDoctorLevel> queryYmDoctorLevel() {
        return this.ymDoctorLevelMapper.queryYmDoctorLevels();
    }

    /**
     * 问医生- 查询医生咨询类型
     *
     */
    @Override
    public List<YmDouctorConsultType> queryYmDouctorConsultType() {
        return this.ymDouctorConsultTypeMapper.queryYmDoctorConsultTypes();
    }
    /**
     * 问医生- 修改医生信息
     *
     *@param  ymDoctor
     *@return Integer
     */
    @Override
    @Transactional
    public Integer updateYmDouctorInfo(Long userId,YmDoctor ymDoctor) {
        Time workStart = ymDoctor.getWorkStart();
        Time workEnd = ymDoctor.getWorkEnd();
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        Assert.notNull(workStart,String.format(ErrorUtil.PARAMETER_NULL,"workStart"));
        Assert.notNull(workEnd,String.format(ErrorUtil.PARAMETER_NULL,"workEnd"));
        Integer res = null;
        Integer number = ymDoctorMapper.updateByPrimaryKeySelective(ymDoctor);
        YmUser ymUser = ymUserMapper.selectByPrimaryKey(userId);
        if(number != 0){
            res = 1;
            Short type = 1;
            ymUser.setType(type);
            Integer result = ymUserMapper.updateByPrimaryKeySelective(ymUser);
            // 根据用户ID查询doctorId
            YmDoctor ymdoctor = ymDoctorMapper.selectByUserId(userId);
            Long doctorId = ymdoctor.getDoctorId();
            // 配置医生上班时间
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            String str = sdf.format(workStart);
            String str1 = sdf.format(workEnd);
            String workTime = str + "-" + str1;
            YmDoctorWork ymDoctorWork = new YmDoctorWork();
            ymDoctorWork.setDoctorId(doctorId);
            ymDoctorWork.setWorkTime(workTime);
            ymDoctorWork.setWorkStart(workStart);
            ymDoctorWork.setWorkEnd(workEnd);
            // 先删除再新增医生上班时间
            ymDoctorWorkMapper.deleteByDoctorId(doctorId);
            ymDoctorWorkMapper.insertSelective(ymDoctorWork);
            if(result != 0){
                res = 1;
            }else{
                res = 0;
            }
        }else{
            res = 0;
        }
        return res;
    }

    /*
     * 根据userId查询医生详细信息
     * */
    @Override
    public YmDoctor queryDoctorInfoByUserId(Long userId) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        return this.ymDoctorMapper.selectByUserId(userId);
    }

    @Override
    public YmDoctor queryDoctorInfoByDoctorId(Long doctorId) {
        return this.ymDoctorMapper.selectByPrimaryKey(doctorId);
    }

    /**
     * 问医生- 平台推荐医生查询(根据提问资料倒序排序)
     *
     */
    @Override
    public List<YmDoctor> queryYmDoctorOrderByQuota() {

        return this.ymDoctorMapper.queryYmDoctorOrderByQuota();
    }
    /**
     * 问医生- 根据当前登录用户所属区域查询推荐医生
     *
     */
    @Override
    public List<YmDoctor> queryYmDoctorOrderByStationId(Long cityAreaId) {
        return this.ymDoctorMapper.queryYmDoctorOrderByStationId(cityAreaId);
    }
    /**
     * 问医生- 查询所有医生
     *
     */
    @Override
    public List<YmDoctor> queryAllYmDoctor() {
        return this.ymDoctorMapper.queryAllYmDoctor();
    }

    /**
     * 问医生-快速提问-根据咨询类型和在线状态查询医生
     * @param
     * @return List<Long>
     */
    public List<Long> queryYmDoctorByDouctorConsultTypeId(Long douctorConsultTypeId){
        return this.ymDoctorMapper.queryYmDoctorByDouctorConsultTypeId(douctorConsultTypeId);
    }

    /**
     * 问医生-查询所有未配置上班时间的医生
     * @param
     * @return List<Long>
     */
    @Override
    public List<YmDoctor> queryAllDoctorByWorkDate() {

        return ymDoctorMapper.queryAllDoctorByWorkDate();
    }
    /**
     * 问医生- 定时任务，重置所有平台推荐医生的可提问数
     * @param
     * @return List<Long>
     */
    @Override
    public Integer updateTotalQuota() {
        // 查询所有的平台推荐医生
       // List<YmDoctor> ymDoctorList = ymDoctorMapper.queryYmDoctorOrderByQuota();
        // 重置所有平台推荐医生的可提问数
        return ymDoctorMapper.updateTotalQuota();
    }
}
